package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.BFont;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class StoreItem extends Sprite
	{
		private var objectConfig:ObjectConfig;
		//背景
		private var tabItemBackground:Sprite;
		//物品具体信息
		private var objectTip:Sprite;
		//能否购买标识
		private var storeItemLock:Sprite;

		
		public function StoreItem(objectConfig:ObjectConfig)
		{
			super();
			this.objectConfig = objectConfig;
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			tabItemBackground = new TabItemBackground();
			tabItemBackground.width = 110;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			
			//标题名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = objectConfig.name;
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 10;
			addChild(titleText);
			//物品图片
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var objectSprite:Sprite = new ObjectImage();
			if (objectConfig.type == 2 || objectConfig.type == 3)
			{
				objectSprite.width = 50;
				objectSprite.height = 50;
				objectSprite.x = (this.width - objectSprite.width) / 2;
				objectSprite.y = (this.height - objectSprite.height) / 2;
			}
			else
			{
				objectSprite.width = 35;
				objectSprite.height = 45;
				objectSprite.x = 28;
				objectSprite.y = 55;
				objectSprite.rotation = -40;
			}
			if (objectConfig.type == 2)
			{	
				var equipConfig:EquipConfig = objectConfig as EquipConfig;
				if (equipConfig.color > 0)
				{
					var gradientGlow:GradientGlowFilter;
					gradientGlow = new GradientGlowFilter();
					gradientGlow.distance = 0;
					gradientGlow.angle = 45;
					gradientGlow.colors = [0xFFFFFF, Config.equipColor2[equipConfig.color]];
					gradientGlow.alphas = [0, 1];
					gradientGlow.ratios = [0, 255];
					gradientGlow.blurX = 20;
					gradientGlow.blurY = 20;
					gradientGlow.strength = 2;
					gradientGlow.quality = BitmapFilterQuality.HIGH;
					gradientGlow.type = BitmapFilterType.OUTER;
					objectSprite.filters = [gradientGlow];
				}
			}
			addChild(objectSprite);
			
			//加载价格图标和价格
			var priceContainer:Sprite = new Sprite();
			//价格图标
			var PriceImage:Class;
			var price:int;
			if (objectConfig.priceGoldBones > 0)
			{
				PriceImage = ResourceHelper.getInstance().getClassByName("PriceGoldBone");
				price = objectConfig.priceGoldBones;
			}
			else 
			{
				PriceImage = ResourceHelper.getInstance().getClassByName("PriceBone");
				price = objectConfig.priceBones;
			}
			var priceImage:Sprite = new PriceImage();
			priceImage.width = 20;
			priceImage.height = 20;
			priceContainer.addChild(priceImage);
			//价格
			var priceText:TextField = new TextField();
			priceText.selectable = false;
			priceText.autoSize = TextFieldAutoSize.LEFT;
			priceText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			priceText.text = price.toString();
			priceText.x = priceImage.x + priceImage.width + 5;
			priceText.y = priceImage.y;
			priceContainer.addChild(priceText);
			
			priceContainer.x = (tabItemBackground.width - priceContainer.width) / 2;
			priceContainer.y = 95;
			this.addChild(priceContainer);
			
			//锁
			var StoreItemLock:Class = ResourceHelper.getInstance().getClassByName("StoreItemLock");
			storeItemLock = new StoreItemLock();
			storeItemLock.width = 60;
			storeItemLock.scaleY = storeItemLock.scaleX;
			storeItemLock.x = 25;
			storeItemLock.y = 61;
			addChild(storeItemLock);
			
			refresh();
			addEventListener(MouseEvent.MOUSE_OVER, addInfomationOnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, removeInfomationOnMouseOut);
			
		}
		
		private function onStoreItemClick(event:MouseEvent):void
		{
			var purchaseDialog:PurchaseDialog = new PurchaseDialog(objectConfig, 0);
			purchaseDialog.x = (stage.stageWidth - purchaseDialog.width) / 2;
			purchaseDialog.y = (stage.stageHeight - purchaseDialog.height) / 2;
			SceneContainer.getInstance().currentScene.addChild(purchaseDialog);
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
		
		//刷新，能够买、不能够买的分开
		public function refresh():void
		{
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[player.ownAnimals[0]];
			if (animalInfo.level < this.objectConfig.requireLevel || animalInfo.intelligence < this.objectConfig.requireInteligenceLevel)
			{
				removeEventListener(MouseEvent.CLICK, onStoreItemClick);
				removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				storeItemLock.visible = true;
			}
			else
			{
				addEventListener(MouseEvent.CLICK, onStoreItemClick);
				addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				storeItemLock.visible = false;
			}
		}
		
		//添加说明信息
		public function addInfomationOnMouseOver(event:MouseEvent):void
		{
			objectTip = new Sprite();
			var roundRec:RoundRec;
			if (objectConfig.type == 2)
 				roundRec = new RoundRec(130, 210, 0.8, BFont.roundRecColor);
 			else
 				roundRec = new RoundRec(130, 150, 0.8, BFont.roundRecColor);
			objectTip.addChild(roundRec);
			//名字
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.width = roundRec.width;
        	nameText.autoSize = TextFieldAutoSize.CENTER;
        	var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			nameTexFormat.leftMargin = 5;
			nameTexFormat.rightMargin = 5;
			nameText.defaultTextFormat = nameTexFormat;
			
			//具体信息
			var tipText:TextField = new TextField();
			tipText.selectable = false;
        	tipText.autoSize = TextFieldAutoSize.LEFT;
        	tipText.width = roundRec.width;
        	tipText.wordWrap = true;
        	var texFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			texFormat.leftMargin = 5;
			texFormat.rightMargin = 5;
			tipText.defaultTextFormat = texFormat;
			
			var qualityText:TextField = new TextField();
			qualityText.selectable = false;
        	qualityText.autoSize = TextFieldAutoSize.LEFT;
        	qualityText.width = roundRec.width;
        	qualityText.wordWrap = true;
			
			var suitEffect:TextField = new TextField();
			suitEffect.selectable = false;
        	suitEffect.autoSize = TextFieldAutoSize.LEFT;
        	suitEffect.width = roundRec.width;
        	suitEffect.wordWrap = true;
			suitEffect.defaultTextFormat = texFormat;
			
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
			if (myAnimal.level >= this.objectConfig.requireLevel && myAnimal.intelligence >= this.objectConfig.requireInteligenceLevel)
			{
				if (objectConfig.priceGoldBones > 0)
				{
					tipText.text = "价格: " + objectConfig.priceBones + "\n"; 
				}
				else
				{
					tipText.text = "价格: " + objectConfig.priceBones + "\n"; 
				}
				//骨头
				if (objectConfig.type == 0)
				{
					tipText.appendText("重量: " + objectConfig.weight  + "\n");
					tipText.appendText("作用: " + objectConfig.description.toString()); 
				}
				//药剂
				else if (objectConfig.type == 1)
				{
					tipText.appendText("作用: " + objectConfig.description.toString());
				}
				//装备
				else if (objectConfig.type == 2)
				{
					var equipConfig:EquipConfig = objectConfig as EquipConfig;
					var magicConfig:MagicConfig = Config.magicConfigs[equipConfig.magicIndex];
					if (equipConfig.durability == -1)
						tipText.appendText("耐久度: 永久\n");
					else
						tipText.appendText("耐久度: " + equipConfig.durability + "\n");
					tipText.appendText("魔法: " + magicConfig.name + "\n");
					tipText.appendText("作用: " + equipConfig.description.toString() + "\n");
					
					if (equipConfig.color > 0)
					{
						var colorTextFormat:TextFormat = new TextFormat("微软雅黑", 13, Config.equipColor1[equipConfig.color], true);
						colorTextFormat.leftMargin = 5;
						colorTextFormat.rightMargin = 5;
						qualityText.y = tipText.y + tipText.height + 20;
						qualityText.defaultTextFormat = colorTextFormat;
						qualityText.text = "品质: " + Config.equipColorName[equipConfig.color] + "  " + equipConfig.colorDescribe;
						var colorNameFormat:TextFormat = new TextFormat("微软雅黑", 15, Config.equipColor1[equipConfig.color], true);
						colorNameFormat.leftMargin = 5;
						colorNameFormat.rightMargin = 5;
						nameText.defaultTextFormat = colorNameFormat;
						suitEffect.y = qualityText.y + qualityText.height - 5;
					}
					else
					{
						suitEffect.y = tipText.y + tipText.height + 20;
					}
					magicConfig = Config.magicConfigs[equipConfig.wholeMagicIndex];
					suitEffect.text = "整套附加: " + equipConfig.wholeEquipDescription.toString();
					suitEffect.appendText("\n整套魔法: " + magicConfig.name);
				}
				//弓
				else if (objectConfig.type == 3)
				{
					tipText.appendText(objectConfig.description);
				}
			}
			else
			{
				if (myAnimal.level < this.objectConfig.requireLevel)
				{
					tipText.appendText("需要等级: " + objectConfig.requireLevel + "\n");
				} 
				if (myAnimal.intelligence < this.objectConfig.requireInteligenceLevel)
				{
					tipText.appendText("需要智力: " + objectConfig.requireInteligenceLevel + "\n"); 
				}
			}
			nameText.text = objectConfig.name.toString();
			nameText.y = 5;
			tipText.y = 25;
			
			objectTip.addChild(nameText);
			objectTip.addChild(tipText);
			objectTip.addChild(qualityText);
			objectTip.addChild(suitEffect);
			if (this.x > 400)
			{
				objectTip.x = this.x - 37;
			}
			else
			{
				objectTip.x = this.x + 200;
			}
			objectTip.y = this.y + 65;
			var store:Store = Store.getInstance() as Store;
			store.addChild(objectTip);
		}
		
		//移除说明信息
		public function removeInfomationOnMouseOut(event:MouseEvent):void
		{
			if (null != objectTip.parent)
			{
				objectTip.parent.removeChild(objectTip);
			}
		}
		
	}
}